/**
 * 2009-10-25 上午09:50:39 
 * esignature
 * com.smc.security
 * AuthenticationManagerImpl
 */
package com.smc.security;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.smc.domain.model.User;
import com.smc.security.util.CoderUtil;
import com.smc.server.AuthenticationService;
import com.smc.server.UserService;

/**
 * @author chenheng
 * @version 0.0.1
 * 认证授权管理器实现
 */
public class AuthenticationManagerImpl implements AuthenticationManager {
	
	private static String USERNAME = "login_name" ;
	private static String PASSWORD = "login_password" ;
	private static String REMEMBERME = "login_rememberme";
	private UserService userService = null ;
	private AuthenticationService authenticationService = null ;
	
	
	public AuthenticationService getAuthenticationService() {
		return authenticationService;
	}

	public void setAuthenticationService(AuthenticationService authenticationService) {
		this.authenticationService = authenticationService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void anonymousAuthentication(HttpServletRequest request,HttpServletResponse response) {
		AuthenticationToken auth = new AuthenticationToken("ANONYMOUS","ANONYMOUS",null);
		SecurityContextHolder.setAuthentication(auth,request.getSession());
	}

	@SuppressWarnings("unchecked")
	public void remembermeAuthentication(HttpServletRequest request,HttpServletResponse response) {
		AuthenticationToken auth = null;
		Cookie[] cookies = request.getCookies();
		String username = null;
		String password = null;
		if ( cookies != null ) {
			for (Cookie cookie : cookies ) {
				if ( cookie.getName().equals(USERNAME)) {
					username = cookie.getValue();
				}
				if ( cookie.getName().equals(PASSWORD)) {
					password = cookie.getValue();
				}
			}
		}
		User user = userService.login_IG_IG_IG_user(username, password);
		List<GrantedAuthority> gal = new ArrayList<GrantedAuthority>(); 
		if ( user != null && user.getUuid()!= null ) {
			List<com.smc.domain.model.Authentication> authes = authenticationService.fetch_IG_IG_IG_authentication(user);
			for (com.smc.domain.model.Authentication a : authes ) {
				GrantedAuthority ga = new GrantedAuthorityImpl(a);
				gal.add(ga);
			}
		}
		if ( gal.size() > 0) {
			GrantedAuthority[] t = new GrantedAuthority[gal.size()];
			auth = new AuthenticationToken(password,username,user.getUuid(),gal.toArray(t));
		} else {
			auth = new AuthenticationToken(password,username,null);
		}
		SecurityContextHolder.setAuthentication(auth,request.getSession());
	}

	@SuppressWarnings("unchecked")
	public void usernamePasswordAuthentication(HttpServletRequest request,HttpServletResponse response) throws Exception {
		AuthenticationToken auth = null ;
		String username = request.getParameter(USERNAME);
		String originPassword = request.getParameter(PASSWORD);
		String rememberme = request.getParameter(REMEMBERME);
		String originstr = username + originPassword ;
		String password = CoderUtil.byte2hex(CoderUtil.encryptMD5((originstr.getBytes())));
		User user = userService.login_IG_IG_IG_user(username, password);
		List<GrantedAuthority> gal = new ArrayList<GrantedAuthority>(); 
		if ( user != null && user.getUuid()!= null ) {
			if ( rememberme.equals("true")) {
				Cookie usernameCookie = new Cookie(USERNAME,username);
				Cookie passwordCookie = new Cookie(PASSWORD,password);
				response.addCookie(usernameCookie);
				response.addCookie(passwordCookie);
			}
			List<com.smc.domain.model.Authentication> authes = authenticationService.fetch_IG_IG_IG_authentication(user);
			for (com.smc.domain.model.Authentication a : authes ) {
				GrantedAuthority ga = new GrantedAuthorityImpl(a);
				gal.add(ga);
			}
		}
		if ( gal.size() > 0) {
			GrantedAuthority[] t  = new GrantedAuthority[gal.size()] ;
			auth = new AuthenticationToken(password,username,user.getUuid(),gal.toArray(t));
		} else {
			auth = new AuthenticationToken(password,username,null);
		}
		SecurityContextHolder.setAuthentication(auth,request.getSession());
	}

	public void logout(HttpServletRequest request, HttpServletResponse response) {
		SecurityContextHolder.removeAuthentication(request.getSession());
		Cookie[] cookies = request.getCookies();
		if ( cookies != null) {
			for (Cookie cookie : cookies) {
				if ( cookie.getName().equals(USERNAME)) {
					cookie.setMaxAge(0);
					cookie.setValue(null);
					response.addCookie(cookie);
				}
				if ( cookie.getName().equals(PASSWORD)) {
					cookie.setMaxAge(0);
					cookie.setValue(null);
					response.addCookie(cookie);
				}
			}
		}
	}

}
